package com.phospherus.flow.service

import com.phospherus.flow.entity.Buyertotcf
import com.phospherus.flow.repository.BuyertotcfRepository
import org.apache.commons.lang.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service

/**
 * 流程2表单数据
 * @author songjinlong
 * @version 1.0
 * @since 2017/12/27
 */
@Service
class BuyertotcfService : FormInstBaseService() {
    @Autowired
    lateinit var repository: BuyertotcfRepository

    override fun getFormInstServiceName(): String = "buyertotcf"

    override fun save(map: MutableMap<String, Any>) {
        var formInstId = map["formInstId"] as Long
        var buyertotcf_bindFContracts = map["buyertotcf_bindFContracts"] as String?
        var buyertotcf_bindDContracts = map["buyertotcf_bindDContracts"] as String?
        var id = (map["id"] as? String)?.toLong()
        var buyertotcf = repository.save(Buyertotcf(id = id, formInstId = formInstId, buyertotcf_bindFContracts = buyertotcf_bindFContracts, buyertotcf_bindDContracts = buyertotcf_bindDContracts))
        var files = map["attachedFiles"] as String?
        if (StringUtils.isNotBlank(files)) {
            attachedFileService.addFormInstAttachedFile(files, formInstId, "buyertotcf", buyertotcf.id)
        }
    }

    override fun query(formInstId: Long): MutableMap<String, Any?> {
        var map = mutableMapOf<String, Any?>()
        var buyertotcf = repository.findByFormInstId(formInstId)
        map["buyertotcf_bindFContracts"] = if (buyertotcf == null) {
            ""
        } else {
            buyertotcf.buyertotcf_bindFContracts
        }
        map["buyertotcf_bindDContracts"] = if (buyertotcf == null) {
            ""
        } else {
            buyertotcf.buyertotcf_bindDContracts
        }
        map["id"] = if (buyertotcf == null) {
            ""
        } else {
            buyertotcf.id
        }
        map["attachedFileList"] = if (buyertotcf == null) {
            ""
        } else {
            attachedFileService.getFormAttachedFiles(formInstId, buyertotcf.id,"buyertotcf")
        }

        return map
    }
}